﻿using System;
using System.Collections.Generic;
using System.Linq;
using MyWebApp.Integration;
using MyWebApp.Integration.AllegroWebApi;


namespace MyWebApp.Data
{
    public class BikesSearchService : IBikesSearchService
    {
        private readonly AllegroSearchService _searchService;

        public BikesSearchService()
        {
            _searchService = new AllegroSearchService();
        }

        public IEnumerable<Bike> GetBikes(string name, int categoryId, int stateId, int pageNumber, out int numberOfItemsFound)
        {
            const int dataPortion = 20;

            SearchResponseType[] searchResponses = _searchService.DoSearch(name, categoryId, stateId, out numberOfItemsFound)
                                                    .Skip((pageNumber - 1) * dataPortion)
                                                    .Take(dataPortion)
                                                    .ToArray();
     
            var searchResponsesDetails = _searchService.GetItemInfos(searchResponses.Select(s => s.sItId).ToArray());

            return from searchResponse in searchResponses
                         join searchResponseDetail in searchResponsesDetails
                         on searchResponse.sItId equals searchResponseDetail.itemInfo.itId
                         select ConvertToBike(searchResponse, searchResponseDetail);
        }

        public IEnumerable<Bike> GetLatestBikes()
        {
            int found;
            return GetBikes("rower", 16420, 0,1, out found).Take(6);
        }

        private Bike ConvertToBike(SearchResponseType searchResponse, ItemInfoStruct searchResponseDetail)
        {
            return new Bike()
            {
                Id = searchResponse.sItId,
                AuctionPlatform = searchResponse.sItAdvertInfo.itemIsAdvert == 6 ? "Tablica.pl" : "Allegro.pl",
                Name = searchResponse.sItName,
                Price = searchResponse.sItPrice == 0 ? searchResponse.sItBuyNowPrice : searchResponse.sItPrice,
                ThumbnailUrl = string.IsNullOrEmpty(searchResponse.sItThumbUrl) ? "img/no_image.png" : searchResponse.sItThumbUrl,
                SmallestThumbnailUrl = string.IsNullOrEmpty(searchResponse.sItThumbUrl) ? "img/no_image.png" : searchResponse.sItThumbUrl,
                PhotoUrl = searchResponseDetail.itemImages.Any() ?  searchResponseDetail.itemImages.First(i => i.imageType == 3).imageUrl : "",
                City = searchResponse.sItCity,
                Description = searchResponseDetail.itemInfo.itDescription,
                DateOfCompletion = UnixTimeStampToDateTime(searchResponse.sItEndingTime).ToShortDateString(),
                Type = Mapper.MapCategory(searchResponse.sItCategoryId),
                Url = string.Format("http://allegro.pl/show_item.php?item={0}",searchResponse.sItId),
                IsActive = searchResponseDetail.itemInfo.itBuyNowActive == 1 ? "active" : "completed",
                NumberOfBids = searchResponseDetail.itemInfo.itBidCount
            };
        }

        public DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }
    }
}